package com.me.myFramework.aop22.aop123;

import com.me.myFramework.aop22.IBuy;
import com.me.myFramework.aop22.Switch;
import com.me.myFramework.aop22.aop122.intercept.method.MethodBeforeAdviceInterceptor122;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * 《AOP-123》：  <br><br>
 *
 * TODO 以下变更点：<ol>
 *     <li>
 *          不重要的修改：使用 {@link org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor Spring MethodBeforeAdviceInterceptor}
 *           替换自定义的 {@link MethodBeforeAdviceInterceptor122}
 *     </li>
 *  </ol>
 *
 * @author ME
 */
public class MyAop123 {

    /**
     * 通过 模板模式 提取增强逻辑：方法执行前、正常返回、异常返回
     */
    public static void main(String[] args) throws Throwable {
        IBuy target = new Switch();

        ProxyFactory123 proxyFactory = new ProxyFactory123(target);
        proxyFactory.addAdvice(new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP1] 就想看看谁买东西了：" + args[0]);
            }
        });
        proxyFactory.addAdvice(new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP2] 记录购买价格 " + returnValue);
            }
        });
        IBuy proxy = (IBuy) proxyFactory.getProxy();
        proxy.buy("Tom");
    }

}
